//
//  RBPublishEvaluateVC.m
//  zhongzhenglu
//
//  Created by 马军凯 on 2023/5/5.
//

#import "RBPublishEvaluateVC.h"
#import "RBUploadTool.h"
#import "RBScrollablePhotoContainer.h"

static NSInteger const maxCount = 3;

@interface RBPublishEvaluateVC ()
<UITextViewDelegate>
//scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
//
@property (nonatomic, strong) UITextView *markTV;
@property (nonatomic, strong) UILabel *placeholderLabel;
//当前选择的
@property (nonatomic, assign) NSInteger chooseIndex;

@property (nonatomic, strong) RBUploadTool *uploadTool;
//
@property (nonatomic, strong) RBScrollablePhotoContainer *photoView;
//是否匿名
@property (nonatomic, strong) UISwitch *anonymous;

@end

@implementation RBPublishEvaluateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.chooseIndex = 4;
    [self setup];
    [self setUI];
}
//
- (void)setup {
    self.title = @"发表评价";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    
    UILabel *rightView = UILabel.new;
    rightView.frame = CGRectMake(0, 0, 60, 30);
    [rightView rbSetCornerRadius:15];
    rightView.text = @"发表";
    rightView.backgroundColor = [UIColor LC_hex:@"#518CFF"];
    rightView.textColor = [UIColor whiteColor];
    rightView.textAlignment = 1;
    rightView.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    [rightView rbAddTapGesWithTarget:self action:@selector(clickPublishEvaluate)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
}
//UI
- (void)setUI {
    //白色背景
    UIView *whiteBackView = [UIView new];
    whiteBackView.backgroundColor = [UIColor whiteColor];
    [whiteBackView rbSetCornerRadius:Zoom(8)];
    [self.scrollView addSubview:whiteBackView];
    [whiteBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.scrollView.mas_top).offset(Zoom(10));
        make.left.mas_equalTo(self.scrollView.mas_left).offset(Zoom(12.5));
        make.right.mas_equalTo(self.scrollView.mas_right).offset(-Zoom(12.5));
        make.width.mas_equalTo(Zoom(350));
        make.bottom.mas_equalTo(self.scrollView.mas_bottom).offset(-TAB_BAR_SAFE_MARGIN - Zoom(10));
    }];
    //商品
    UIView *goodsView = [self createGoodsView];
    [whiteBackView addSubview:goodsView];
    [goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(whiteBackView.mas_left).offset(0);
        make.right.mas_equalTo(whiteBackView.mas_right).offset(0);
        make.top.mas_equalTo(whiteBackView.mas_top).offset(20);
    }];
    //评分
    UIView *scopeView = [self createGoodsScopeView];
    [whiteBackView addSubview:scopeView];
    [scopeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(whiteBackView.mas_left).offset(0);
        make.right.mas_equalTo(whiteBackView.mas_right).offset(0);
        make.top.mas_equalTo(goodsView.mas_bottom).offset(22);
    }];
    //留言
    UIView *markView = [self createMarkView];
    [whiteBackView addSubview:markView];
    [markView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(whiteBackView.mas_left).offset(0);
        make.right.mas_equalTo(whiteBackView.mas_right).offset(0);
        make.top.mas_equalTo(scopeView.mas_bottom).offset(30);
    }];
    RBScrollablePhotoContainer *view = [RBScrollablePhotoContainer viewWithMaxNum:maxCount itemWidth:Zoom(78) itemHeight:Zoom(78) itemPadding:Zoom(10) editable:YES];
    view.backgroundColor = [UIColor whiteColor];
    view.bgColor = [UIColor whiteColor];
    self.photoView = view;
    //collectionView
    [whiteBackView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(whiteBackView.mas_left).offset(Zoom(15));
        make.right.mas_equalTo(whiteBackView.mas_right).offset(-Zoom(15));
        make.height.mas_equalTo(Zoom(78));
        make.top.mas_equalTo(markView.mas_bottom).offset(20);
    }];
    //匿名
    UIView *anonymousView = [self createAnonymousView];
    [whiteBackView addSubview:anonymousView];
    [anonymousView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(whiteBackView.mas_left).offset(0);
        make.right.mas_equalTo(whiteBackView.mas_right).offset(0);
        make.top.mas_equalTo(view.mas_bottom).offset(20);
        make.bottom.mas_equalTo(whiteBackView.mas_bottom).offset(-15);
    }];
}


//发表评论
- (void)clickPublishEvaluate {
    if (self.chooseIndex == -1) {
        [self.view showTip:@"请进行商品评分" completion:nil];
    } else {
        [self uploadPhotoData];
    }
}
//点击星星
- (void)clickStarBtn:(UIButton *)sender {
    NSInteger tag = sender.tag - 2000;
    if (self.chooseIndex == tag) {
        for (int i = 0; i < 5; i++) {
            UIButton *btn = [self.scrollView viewWithTag:2000 + i];
            if (i <= tag) {
                btn.selected = !btn.selected;
            } else {
                btn.selected = NO;
            }
        }
        self.chooseIndex = -1;
    } else {
        for (int i = 0; i < 5; i++) {
            UIButton *btn = [self.scrollView viewWithTag:2000 + i];
            if (i <= tag) {
                btn.selected = YES;
            } else {
                btn.selected = NO;
            }
        }
        self.chooseIndex = tag;
    }
}


//上传图片
- (void)uploadPhotoData {
    __weak typeof(self) weak_self = self;
    
    NSArray *picArray = self.photoView.getImages;
    
    if (picArray.count > 0) {
        [self.uploadTool uploadData:({
            NSMutableArray *array = NSMutableArray.array;
            for (NSString *path in picArray) {
                RBUploadModel *model = RBUploadModel.new;
                model.source = path;
                [array addObject:model];
            }
            array;
        }) withCompletion:^(NSArray *data) {
            [weak_self publishEvalueteDataWith:[data componentsJoinedByString:@","]];
        }];
    } else {
        [self publishEvalueteDataWith:@""];
    }
}
//评论
- (void)publishEvalueteDataWith:(NSString *)picurl {
    NSDictionary *params = @{@"order_goods_id" : self.goodsModel.order_goods_id,
                             @"star_num" : [NSString stringWithFormat:@"%zd", (self.chooseIndex + 1)],
                             @"content" : self.markTV.text,
                             @"multiple_file" : picurl,
                             @"video" : @"",
                             @"attr" : @"",
                             @"is_anonymous" : self.anonymous.on == YES ? @"1" : @"0"};
    NSArray *goodsSet = @[params];
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"express_content"] = self.markTV.text;
        dicM[@"goods_set"] = [goodsSet mj_JSONString];
    } path:@"api/goods_evaluate" post:YES log:YES responseClass:RBPublishEvaluateResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBPublishEvaluateResponse *data) {
        if (weakSelf.block) {
            weakSelf.block();
        }
        [weakSelf.view showTip:data.message completion:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    } failBlock:^(RBPublishEvaluateResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}


#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length == 0) {
        self.placeholderLabel.hidden = NO;
    } else {
        self.placeholderLabel.hidden = YES;
    }
}

#pragma mark 视图
//scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [RBThemeConfig bgGrayColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        
        [self.view addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
    }
    return _scrollView;
}
//商品
- (UIView *)createGoodsView {
    UIView *view = [UIView new];
    //图片
    UIImageView *imageView = [UIImageView new];
    [imageView setImageWithUrl:self.goodsModel.picurl placeHolder:nil];
    [imageView rbSetCornerRadius:8];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view.mas_left).offset(15);
        make.top.mas_equalTo(view.mas_top).offset(0);
        make.bottom.mas_equalTo(view.mas_bottom).offset(0);
        make.width.height.mas_equalTo(97);
    }];
    //标题
    UILabel *nameLabel = UILabel.new;
    nameLabel.text = self.goodsModel.title;
    nameLabel.numberOfLines = 2;
    nameLabel.textColor = [UIColor LC_hex:@"#333333"];
    nameLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    [view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageView.mas_right).offset(20);
        make.right.mas_equalTo(view.mas_right).offset(-15);
        make.top.mas_equalTo(view.mas_top).offset(6.5);
    }];
    if (self.goodsModel.type == 1) {
        //价格
        UILabel *priceLabel = [UILabel new];
        priceLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        priceLabel.textColor = [UIColor LC_hex:@"#F25B50"];
        priceLabel.attributedText = [NSString changeSectionFontWithString:[NSString stringWithFormat:@"¥%@", self.goodsModel.price] font:[UIFont systemFontOfSize:11 weight:UIFontWeightMedium] from:0 lenth:1];
        [view addSubview:priceLabel];
        [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageView.mas_right).offset(20);
            make.right.mas_equalTo(view.mas_right).offset(-15);
            make.bottom.mas_equalTo(view.mas_bottom).offset(-13);
        }];
    }
    
    return view;
}
//星级
- (UIView *)createGoodsScopeView {
    UIView *view = UIView.new;
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"商品评分";
    titleLabel.textColor = [UIColor LC_hex:@"#202224"];
    titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    [view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view.mas_left).offset(15);
        make.top.mas_equalTo(view.mas_top).offset(0);
        make.bottom.mas_equalTo(view.mas_bottom).offset(0);
    }];
    //
    UIView *starBackView = [UIView new];
    [view addSubview:starBackView];
    [starBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleLabel.mas_right).offset(10);
        make.top.mas_equalTo(view.mas_top).offset(0);
        make.right.mas_equalTo(view.mas_right).offset(-15);
        make.bottom.mas_equalTo(view.mas_bottom).offset(0);
        make.height.mas_equalTo(Zoom(20));
    }];
    
    for (int i = 0; i < 5; i++) {
        UIButton *starBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        starBtn.frame = CGRectMake(Zoom(30) * i, 0, Zoom(20), Zoom(20));
        [starBtn setBackgroundImage:[UIImage imageNamed:@"star_normal"] forState:UIControlStateNormal];
        [starBtn setBackgroundImage:[UIImage imageNamed:@"star_select"] forState:UIControlStateSelected];
        starBtn.selected = YES;
        starBtn.tag = 2000 + i;
        [starBtn addTarget:self action:@selector(clickStarBtn:) forControlEvents:UIControlEventTouchUpInside];
        [starBackView addSubview:starBtn];
    }
    
    return view;
}
//留言
- (UIView *)createMarkView {
    UIView *view = UIView.new;
    
    self.markTV = [[UITextView alloc] init];
    self.markTV.font = [UIFont systemFontOfSize:14];
    self.markTV.textColor = [UIColor LC_hex:@"#202224"];
    self.markTV.delegate = self;
    [view addSubview:self.markTV];
    [self.markTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_top).offset(0);
        make.left.mas_equalTo(view.mas_left).offset(15);
        make.right.mas_equalTo(view.mas_right).offset(-15);
        make.bottom.mas_equalTo(view.mas_bottom).offset(0);
        make.height.mas_equalTo(Zoom(80));
    }];
    
    self.placeholderLabel = UILabel.new;
    self.placeholderLabel.text = @"商品怎么样？说说体验心得吧~";
    self.placeholderLabel.textColor = [UIColor LC_hex:@"#A2A2A2"];
    self.placeholderLabel.font = [UIFont systemFontOfSize:14];
    [self.markTV addSubview:self.placeholderLabel];
    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.markTV.mas_left).offset(5);
        make.right.mas_equalTo(self.markTV.mas_right).offset(-5);
        make.top.mas_equalTo(self.markTV.mas_top).offset(8);
    }];
    
    return view;
}
//匿名
- (UIView *)createAnonymousView {
    UIView *view = UIView.new;
     
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"匿名评论";
    titleLabel.textColor = [UIColor LC_hex:@"#202224"];
    titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    [view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view.mas_left).offset(15);
        make.center.mas_equalTo(view);
    }];
    
    UISwitch *btn = UISwitch.new;
    btn.onTintColor = [RBThemeConfig priceRedColor];
    [view addSubview:btn];
    self.anonymous = btn;
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_top).offset(0);
        make.right.mas_equalTo(view.mas_right).offset(-15);
        make.bottom.mas_equalTo(view.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
    return view;
}

//上传图片的工具
- (RBUploadTool *)uploadTool {
    if (!_uploadTool) {
        self.uploadTool = RBUploadTool.new;
    }
    return _uploadTool;
}

@end
